package cn.com.utt.common.vo;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

@SuppressWarnings("serial")
public class User implements UserDetails {
	
	private Long id;
	private String email;
	private String username;
	private String password;
	private Long groupId;
	private Long parentId;
	private Date lastlogin;
	private Date lastfail;
	private Date createTime;
	private Date lastResetDate;
	private Short failtimes;
	private Integer smsVolume;
	private List<Role> auths;
	private String remark;
	private String domain;

	public List<Role> getAuths() {
		return auths;
	}

	public void setAuths(List<Role> auths) {
		this.auths = auths;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getLastlogin() {
		return lastlogin;
	}

	public void setLastlogin(Date lastlogin) {
		this.lastlogin = lastlogin;
	}

	public Date getLastfail() {
		return lastfail;
	}

	public void setLastfail(Date lastfail) {
		this.lastfail = lastfail;
	}

	public Short getFailtimes() {
		return failtimes;
	}

	public void setFailtimes(Short failtimes) {
		this.failtimes = failtimes;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (auths == null || auths.size() < 1) {
			return AuthorityUtils.commaSeparatedStringToAuthorityList("");
		}
		StringBuilder commaBuilder = new StringBuilder();
		for (Role role : auths) {
			commaBuilder.append(role.getName()).append(",");
		}
		String authorities = commaBuilder.substring(0, commaBuilder.length() - 1);
		return AuthorityUtils.commaSeparatedStringToAuthorityList(authorities);
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Integer getSmsVolume() {
		return smsVolume;
	}

	public void setSmsVolume(Integer smsVolume) {
		this.smsVolume = smsVolume;
	}

	public Date getLastResetDate() {
		return lastResetDate;
	}

	public void setLastResetDate(Date lastResetDate) {
		this.lastResetDate = lastResetDate;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public boolean hasRole(String name) {
		for (Role role : auths) {
			if (role.getName().equals(name)) {
				return true;
			}
		}
		return false;
	}

}
